/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.myapp.struts.util;

import java.util.ResourceBundle;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * Hibernate Utility class with a convenient method to get Session Factory
 * object.
 *
 * @author Administrator
 */
public class NewHibernateUtil {

    private static final SessionFactory sessionFactory;
    private static final Configuration cfg = new Configuration();
    private static final ResourceBundle rb = ResourceBundle.getBundle("config");
    private static String configFile = "";
    
    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            configFile = rb.getString("hibernate_config_file");
            cfg.configure(configFile);
            cfg.setProperty("hibernate.connection.driver_class", cfg.getProperty("hibernate.connection.driver_class"));
            cfg.setProperty("hibernate.connection.url", cfg.getProperty("hibernate.connection.url"));
            cfg.setProperty("hibernate.connection.username", cfg.getProperty("hibernate.connection.username"));
            cfg.setProperty("hibernate.connection.password", cfg.getProperty("hibernate.connection.password"));
            sessionFactory = cfg.buildSessionFactory();
        } catch (HibernateException ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}
